package cn.com.pagoda.ideplugin.service.pwdGen;

import cn.com.pagoda.ideplugin.service.pwdGen.service.PasswordType;
import cn.com.pagoda.ideplugin.service.pwdGen.service.PwdReq;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 程序入口
 * @author Mch
 * @date 2023-05-27
 */
public class PasswordGeneratorEntry {

    // demo
    public static void main(String[] args) {
        PwdReq pwdReq = new PwdReq();
        pwdReq.setTotal(25);
        Map<String, Integer> generators = new HashMap<>();
        generators.put("LOWER", 2);
        generators.put("UPPER", 15);
        generators.put("NUMBER", 0);
        generators.put("SPECIAL", null);
        pwdReq.setGenerators(generators);
        String generator = new PasswordGeneratorEntry().createGenerator(pwdReq);
        System.out.println(generator);
    }

    /**
     * 请求入口
     */
    public String createGenerator(PwdReq pwdReq) {
        pwdReq.checkData();
        pwdReq.resetRemainNum();
        Map<String, Integer> generators = pwdReq.getGenerators();
        StringBuilder sb = new StringBuilder();
        generators.forEach((k, v) ->  sb.append(PasswordType.trans(k).generatePassword(v)));
        return shuffle(sb).toString();
    }

    private static StringBuilder shuffle(StringBuilder sb) {
        char[] charArray = sb.toString().toCharArray();
        Random random = new Random();
        for (int i = charArray.length - 1; i > 0; i--) {
            int j = random.nextInt(i + 1);
            char temp = charArray[i];
            charArray[i] = charArray[j];
            charArray[j] = temp;
        }
        return new StringBuilder(new String(charArray));
    }



}